package cn.tedu._05mvcboot02.common.exception;

import cn.tedu._05mvcboot02.common.response.JsonResult;
import cn.tedu._05mvcboot02.common.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler
    @ResponseBody
    public JsonResult handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("IllegalArgumentException : " + e.getMessage());
        return new JsonResult(StatusCode.OPERATION_FAILED, e.getMessage());
    }

    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e) {
        log.error("Exception : " + e.getMessage());
        return new JsonResult(StatusCode.THROWABLE_ERROR, e.getMessage());
    }
}
